<?php

use Illuminate\Database\Capsule\Manager as DB;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

#[店铺参数设置管理]
class Dianpu_ConfigController extends AcommonController
{

    public function init()
    {
        $this->model      = (new configModel());
        $this->table      = 'sys_config';
        $this->primaryKey = 'id';
        parent::init();
    }

    #[参数设置]
    public function configAction()
    {
        if(!$this->isPost()) {
            $configAllObject = $this->model->where('type', 29)->get();
            $dataset = [
                //店铺可提现银行
                'merchant_order_settlement_cycle'=>[
                    'title' => '店铺订单结算周期',
                    'value' => 0,
                ],
                //店铺订单平台抽成比例
                'merchant_platform_cut_ratio'=>[
                    'title' => '店铺订单平台抽成比例',
                    'value' => 0
                ],
                //店铺提现手续费费率
                'merchant_withdraw_fee_ratio'=>[
                    'title' => '店铺提现手续费费率',
                    'value' => 0
                ],
                //店铺提现方式
                'merchant_withdraw_type'=>[
                    'title' => '店铺提现方式',
                    'value' => [],
                    'items' => ['微信零钱','支付宝','手动线下打款'],
                ],
                //店铺可提现银行
                'merchant_withdraw_bank'=>[
                    'title' => '店铺提现方式',
                    'value' => '',
                ],
            ];
            if(!empty($configAllObject)){
                foreach ($configAllObject as $config){
                    if ($config['key'] === 'merchant_platform_cut_ratio'){
                        $dataset['merchant_platform_cut_ratio']['value'] = $config->value;
                    }
                    if ($config['key'] === 'merchant_withdraw_fee_ratio'){
                        $dataset['merchant_withdraw_fee_ratio']['value'] = $config->value;
                    }
                    if ($config['key'] === 'merchant_withdraw_type'){
                        $dataset['merchant_withdraw_type']['value'] = $config->value;
                        if(!empty($config->items)){
                            $dataset['merchant_withdraw_type']['items'] = $config->items;
                        }
                    }
                    if ($config['key'] === 'merchant_withdraw_bank'){
                        $dataset['merchant_withdraw_bank']['value'] = $config->value;
                    }
                    if ($config['key'] === 'merchant_order_settlement_cycle'){
                        $dataset['merchant_order_settlement_cycle']['value'] = $config->value;
                    }
                }
            }

            $this->_view->assign('dataset', json_encode($dataset));
            $this->_view->display(strtolower($this->controller) . '/config.html');
        }else{
            $params = $this->postData;
            try {
                DB::beginTransaction();
                $merchant_order_settlement_cycle = $this->model->where('type', 29)->where('key', 'merchant_order_settlement_cycle')->first();
                if($merchant_order_settlement_cycle){
                    $merchant_order_settlement_cycle->value = $params['merchant_order_settlement_cycle'];
                    $merchant_order_settlement_cycle->save();
                }else{
                    $merchant_order_settlement_cycle_data = [
                        'type'  => 29,
                        'title' => '店铺订单结算周期',
                        'key'   => 'merchant_order_settlement_cycle',
                        'value' => $params['merchant_order_settlement_cycle'],
                        'tips'=>'天',
                        'input_mode'=>'text',
                        'items'=>null,
                        'weigh'=>2000,
                        'created_at'=>timestamp(),
                        'updated_at'=>timestamp(),
                    ];
                    configModel::query()->insert($merchant_order_settlement_cycle_data);
                }
                $merchant_platform_cut_ratio = $this->model->where('type', 29)->where('key', 'merchant_platform_cut_ratio')->first();
                if($merchant_platform_cut_ratio){
                    $merchant_platform_cut_ratio->value = $params['merchant_platform_cut_ratio'];
                    $merchant_platform_cut_ratio->save();
                }else{
                    $merchant_platform_cut_ratio_data = [
                        'type'  => 29,
                        'title' => '店铺订单平台抽成比例',
                        'key'   => 'merchant_platform_cut_ratio',
                        'value' => $params['merchant_platform_cut_ratio'],
                        'tips'=>'%',
                        'input_mode'=>'text',
                        'items'=>null,
                        'weigh'=>1000,
                        'created_at'=>timestamp(),
                        'updated_at'=>timestamp(),
                    ];
                    configModel::query()->insert($merchant_platform_cut_ratio_data);
                }
                $merchant_withdraw_fee_ratio = $this->model->where('type', 29)->where('key', 'merchant_withdraw_fee_ratio')->first();
                if($merchant_withdraw_fee_ratio){
                    $merchant_withdraw_fee_ratio->value = $params['merchant_withdraw_fee_ratio'];
                    $merchant_withdraw_fee_ratio->save();
                }else{
                    $merchant_withdraw_fee_ratio_data = [
                        'type'  => 29,
                        'title' => '店铺提现手续费费率',
                        'key'   => 'merchant_withdraw_fee_ratio',
                        'value' => $params['merchant_withdraw_fee_ratio'],
                        'tips'=>'%',
                        'input_mode'=>'text',
                        'items'=>null,
                        'weigh'=>800,
                        'created_at'=>timestamp(),
                        'updated_at'=>timestamp(),
                    ];
                    configModel::query()->insert($merchant_withdraw_fee_ratio_data);
                }
                $merchant_withdraw_type = $this->model->where('type', 29)->where('key', 'merchant_withdraw_type')->first();
                if($merchant_withdraw_type){
                    $merchant_withdraw_type->value = json_encode($params['merchant_withdraw_type']);
                    $merchant_withdraw_type->save();
                }else{
                    $merchant_withdraw_type_data = [
                        'type'  => 29,
                        'title' => '店铺提现方式',
                        'key'   => 'merchant_withdraw_type',
                        'value' => json_encode($params['merchant_withdraw_type']),
                        'tips'=>'',
                        'input_mode'=>'checkbox',
                        'items'=>implode(',', ['微信零钱','支付宝','手动线下打款']),
                        'weigh'=>850,
                        'created_at'=>timestamp(),
                        'updated_at'=>timestamp(),
                    ];
                    configModel::query()->insert($merchant_withdraw_type_data);
                }
                $merchant_withdraw_bank = $this->model->where('type', 29)->where('key', 'merchant_withdraw_bank')->first();
                if($merchant_withdraw_bank){
                    $merchant_withdraw_bank->value = $params['merchant_withdraw_bank'];
                    $merchant_withdraw_bank->save();
                }else{
                    $merchant_withdraw_bank_data = [
                        'type'  => 29,
                        'title' => '店铺可提现银行',
                        'key'   => 'merchant_withdraw_bank',
                        'value' => $params['merchant_withdraw_bank'],
                        'tips'=>'多个使用英文逗号隔开',
                        'input_mode'=>'textarea',
                        'items'=>'',
                        'weigh'=>840,
                        'created_at'=>timestamp(),
                        'updated_at'=>timestamp(),
                    ];
                    configModel::query()->insert($merchant_withdraw_bank_data);
                }
                unset($merchant_order_settlement_cycle, $merchant_order_settlement_cycle_data, $merchant_platform_cut_ratio_data, $merchant_platform_cut_ratio, $merchant_withdraw_fee_ratio, $merchant_withdraw_fee_ratio_data, $merchant_withdraw_type, $merchant_withdraw_type_data, $params);
                DB::commit();
                ret(0, '店铺设置成功');
            } catch (Exception $e){
                DB::rollback();
                ret(20, '店铺设置失败');
            } finally {
                unset($params);
            }
        }
    }

}
